{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "view-in-github"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/DeepLabCut/DeepLabCut/blob/main/examples/COLAB/COLAB_DLC_ModelZoo.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "RK255E7YoEIt"
      },
      "source": [
        "# DeepLabCut Model Zoo user-contributed models\n",
        "\n",
        "🚨 **WARNING** -- This is using the old version from 2020-2023 with user-supplied models. Please see the SuperAnimal notebook if you want to use our Foundational Models for Quadrupeds or mice.\n",
        "\n",
        "![alt text](https://images.squarespace-cdn.com/content/v1/57f6d51c9f74566f55ecf271/1616492373700-PGOAC72IOB6AUE47VTJX/ke17ZwdGBToddI8pDm48kB8JrdUaZR-OSkKLqWQPp_YUqsxRUqqbr1mOJYKfIPR7LoDQ9mXPOjoJoqy81S2I8N_N4V1vUb5AoIIIbLZhVYwL8IeDg6_3B-BRuF4nNrNcQkVuAT7tdErd0wQFEGFSnBqyW03PFN2MN6T6ry5cmXqqA9xITfsbVGDrg_goIDasRCalqV8R3606BuxERAtDaQ/modelzoo.png?format=1000w)\n",
        "\n",
        "http://modelzoo.deeplabcut.org\n",
        "\n",
        "You can use this notebook to analyze videos with pretrained networks from our model zoo - NO local installation of DeepLabCut is needed!\n",
        "\n",
        "- **What you need:** a video of your favorite dog, cat, human, etc: check the list of currently available models here: http://modelzoo.deeplabcut.org\n",
        "\n",
        "- **What to do:** (1) in the top right corner, click \"CONNECT\". Then, just hit run (play icon) on each cell below and follow the instructions!\n",
        "\n",
        "## **Please consider giving back and labeling a little data to help make each network even better!**\n",
        "\n",
        "We have a WebApp, so no need to install anything, just a few clicks! We'd really appreciate your help!\n",
        "   \n",
        "https://contrib.deeplabcut.org/\n",
        "\n",
        "\n",
        "- **Note, if you performance is less that you would like:** firstly check the labeled_video parameters (i.e. \"pcutoff\" in the config.yaml file that will set the video plotting) - see the end of this notebook. You can also use the model in your own projects locally. Please be sure to cite the papers for the model, and http://modelzoo.deeplabcut.org (paper forthcoming!)\n",
        "\n",
        "\n",
        "\n",
        "\n",
        "\n",
        "## **Let's get going: install DeepLabCut into COLAB:**\n",
        "\n",
        "*Also, be sure you are connected to a GPU: go to menu, click Runtime > Change Runtime Type > select \"GPU\"*\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Install the latest version of DeepLabCut\n",
        "!pip install --pre \"deeplabcut[tf,modelzoo]\""
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### Important - Restart the Runtime for the updated packages to be imported!\n",
        "\n",
        "PLEASE, click \"restart runtime\" from the output above before proceeding!"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ZT4PwGSbYQEO"
      },
      "source": [
        "## Now let's set the backend & import the DeepLabCut package\n",
        "### (if colab is buggy/throws an error, just rerun this cell):"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "bvoiWefrYQEP"
      },
      "outputs": [],
      "source": [
        "import os\n",
        "import deeplabcut"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "syweXs88tyuO"
      },
      "source": [
        "## Next, run the cell below to upload your video file from your computer:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "7eqEZYs_CaLy"
      },
      "outputs": [],
      "source": [
        "from google.colab import files\n",
        "\n",
        "uploaded = files.upload()\n",
        "for filepath, content in uploaded.items():\n",
        "  print(f'User uploaded file \"{filepath}\" with length {len(content)} bytes')\n",
        "video_path = os.path.abspath(filepath)\n",
        "\n",
        "# If this cell fails (e.g., when using Safari in place of Google Chrome),\n",
        "# manually upload your video via the Files menu to the left\n",
        "# and define `video_path` yourself with right click > copy path on the video."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "YsaqOTkZtf-w"
      },
      "source": [
        "## Select your model from the dropdown menu, then below (optionally) input the name you want for the project:\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "Ih0t7lUjYQEd"
      },
      "outputs": [],
      "source": [
        "import ipywidgets as widgets\n",
        "from IPython.display import display\n",
        "\n",
        "model_options = deeplabcut.create_project.modelzoo.Modeloptions\n",
        "model_selection = widgets.Dropdown(\n",
        "    options=model_options,\n",
        "    value=model_options[0],\n",
        "    description=\"Choose a DLC ModelZoo model!\",\n",
        "    disabled=False\n",
        ")\n",
        "display(model_selection)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "UV0QXswGCFrI"
      },
      "outputs": [],
      "source": [
        "project_name = 'myDLC_modelZoo'\n",
        "your_name = 'teamDLC'\n",
        "model2use = model_selection.value\n",
        "videotype = os.path.splitext(video_path)[-1].lstrip('.') #or MOV, or avi, whatever you uploaded!"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "JQxko-t3uMVO"
      },
      "source": [
        "## Attention on this step !!\n",
        "- Please note that for optimal performance your videos should contain frames that are around ~300-600 pixels (on one edge). If you have a larger video (like from an iPhone, first downsize by running this please! :)\n",
        "\n",
        "- Thus, if you're using an iPhone, or such, you'll need to downsample the video first by running the code below**\n",
        "\n",
        "(no need to edit it unless you want to change the size)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "WpAX3BKY94e0"
      },
      "outputs": [],
      "source": [
        "video_path = deeplabcut.DownSampleVideo(video_path, width=300)\n",
        "print(video_path)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "KJm_Vbx-s5OY"
      },
      "source": [
        "## Lastly, run the cell below to create a pretrained project, analyze your video with your selected pretrained network, plot trajectories, and create a labeled video!:\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "T9MGgAdIFKPY"
      },
      "outputs": [],
      "source": [
        "config_path, train_config_path = deeplabcut.create_pretrained_project(\n",
        "    project_name,\n",
        "    your_name,\n",
        "    [video_path],\n",
        "    videotype=videotype,\n",
        "    model=model2use,\n",
        "    analyzevideo=True,\n",
        "    createlabeledvideo=True,\n",
        "    copy_videos=True, #must leave copy_videos=True\n",
        "    engine=deeplabcut.Engine.TF,\n",
        ")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "WS-KxhBMvEBj"
      },
      "source": [
        "Now, you can move this project from Colab (i.e. download it to your GoogleDrive), and use it like a normal standard project!\n",
        "\n",
        "You can analyze more videos, extract outliers, refine then, and/or then add new key points + label new frames, and retrain if desired. We hope this gives you a good launching point for your work!\n",
        "\n",
        "###Happy DeepLabCutting! Welcome to the Zoo :)\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "KPOqiLmo6d7t"
      },
      "source": [
        "## More advanced options:\n",
        "\n",
        "- If you would now like to customize the video/plots - i.e., color, dot size, threshold for the point to be plotted (pcutoff), please simply edit the \"config.yaml\" file by updating the values below:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "yGLNVK1q6rIp"
      },
      "outputs": [],
      "source": [
        "# Updating the plotting within the config.yaml file (without opening it ;):\n",
        "edits = {\n",
        "    'dotsize': 7,  # size of the dots!\n",
        "    'colormap': 'spring',  # any matplotlib colormap!\n",
        "    'pcutoff': 0.5,  # the higher the more conservative the plotting!\n",
        "}\n",
        "deeplabcut.auxiliaryfunctions.edit_config(config_path, edits)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "Vlc0wZgB7R5e"
      },
      "outputs": [],
      "source": [
        "# re-create the labeled video (first you will need to delete in the folder to the LEFT!):\n",
        "project_path = os.path.dirname(config_path)\n",
        "full_video_path = os.path.join(\n",
        "    project_path,\n",
        "    'videos',\n",
        "    os.path.basename(video_path),\n",
        ")\n",
        "\n",
        "#filter predictions (should already be done above ;):\n",
        "deeplabcut.filterpredictions(config_path, [full_video_path], videotype=videotype)\n",
        "\n",
        "#re-create the video with your edits!\n",
        "deeplabcut.create_labeled_video(config_path, [full_video_path], videotype=videotype, filtered=True)"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "include_colab_link": true,
      "name": "Copy of COLAB_DLC_ModelZoo.ipynb",
      "provenance": [],
      "toc_visible": true
    },
    "gpuClass": "standard",
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.7"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
